#include "videomodel.h"
VideoModel* VideoModel::instance = nullptr;

VideoModel *VideoModel::getInstance()
{
    if(VideoModel::instance == nullptr)
    {
        VideoModel::instance = new VideoModel;
    }
    return VideoModel::instance;
}

int VideoModel::InsertVideo(QString videoPath, QString videoheadPath, QString videodate, int fps, int userId)
{
    QString sql = QString ("insert into tbl_video(video_path, video_headpath, video_date, video_fps, user_id) values ('%1', '%2', '%3', %4, %5);").arg(videoPath).arg(videoheadPath).arg(videodate).arg(fps).arg(userId);
    int res = SingletonBD::getInstance()->doUpdateInsertDel(sql);
    return res;
}

int VideoModel::selectVideo(char **&qres, int &row, int &col)
{
    UserModel::getInstance()->selectUserId(qres, row, col);
    QString id = qres[1];
    qDebug() << "userId" << id;

    qres = nullptr;
    col=0,row=0;
    QString sql = QString("SELECT video_path, video_headpath, video_date FROM tbl_video a INNER JOIN tbl_user b ON a.user_id = b.user_id WHERE a.user_id = %1 ORDER BY video_date DESC;").arg(id);
    int res = SingletonBD::getInstance()->doSelect(sql, qres, row, col);
    if(res==0){
        if(row>0){  //有数据，更新设备信息
            qDebug() << "有结果";
            return 0;
        }
    }
    return -1;
}

int VideoModel::selectFps(QString path, char**& qres, int &row, int &col)
{
    QString sql = QString("SELECT video_fps FROM tbl_video WHERE video_path = '%1' ORDER BY video_date DESC;").arg(path);
//    char **qres = nullptr;
//    int col=0,row=0;
    int res = SingletonBD::getInstance()->doSelect(sql, qres, row, col);
    if(res==0){
        if(row>0){  //有数据，更新设备信息
            qDebug() << "有结果";
            return 0;
        }
    }
    return -1;
}

//int VideoModel::selectVideoList(QString path, char **&qres, int &row, int &col)
//{
//    QString sql = QString("SELECT video_path, video_id FROM tbl_video a INNER JOIN tbl_user b \
//                          ON a.user_id = b.user_id      \
//                          WHERE a.user_id = 2           \
//                          ORDER BY a.user_id DESC;");
//}

VideoModel::VideoModel()
{

}
